package com.wooming.myserver.service.impl;

import com.wooming.myserver.constant.GameConst;
import com.wooming.myserver.constant.GameStageConst;
import com.wooming.myserver.dto.GameDataDTO;
import com.wooming.myserver.dto.HumanResourceDTO;
import com.wooming.myserver.dto.HumanResourceSureDTO;
import com.wooming.myserver.entity.HumanResource;
import com.wooming.myserver.mapper.GameDataMapper;
import com.wooming.myserver.mapper.HumanResourceMapper;
import com.wooming.myserver.service.inter.HumanResourceService;
import com.wooming.myserver.util.PercentRandom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 人力资源服务实现类
 *
 * @author Wooming 2025/05/28
 */
@Service
public class HumanResourceServiceImpl implements HumanResourceService {

    private final HumanResourceMapper humanResourceMapper;
    private final GameDataMapper gameDataMapper;

    @Autowired
    public HumanResourceServiceImpl(
            HumanResourceMapper humanResourceMapper,
            GameDataMapper gameDataMapper
    ) {
        this.humanResourceMapper = humanResourceMapper;
        this.gameDataMapper = gameDataMapper;
    }

    @Override
    public List<HumanResource> getByUserId(Integer userId) {
        // 获取数据库中的数据
        List<HumanResourceDTO> originalList = humanResourceMapper.getAll(userId);

        // 将数据转换为实体对象并返回
        return originalList.stream()
                .map(this::convertToEntity)
                .toList();
    }

    private HumanResource convertToEntity(HumanResourceDTO dto) {
        return new HumanResource(
                dto.getId(),
                dto.getName(),
                dto.getLevel(),
                dto.getMaxLevel(),
                dto.getTrainCost(),
                dto.getActionPoint(),
                dto.getSalary()
        );
    }

    @Override
    public List<HumanResource> generate(Integer userId, Integer stage) {
        // 判断资金是否允许
        GameDataDTO gameDataDTO = gameDataMapper.getGameData(userId);
        if (gameDataDTO.getGold() < GameConst.HUMAN_RESOURCE_GENERATE_NEED_GOLD.getValue()) return null;

        // 消耗资金
        gameDataDTO.setGold(gameDataDTO.getGold() - 1000);
        gameDataMapper.updateGameData(gameDataDTO);

        // 删除临时数据
        humanResourceMapper.deleteTemp(userId);

        for (int i = 0; i < GameConst.HUMAN_RESOURCE_NUMBER.getValue(); i++) {
            // 生成数据并暂存
            HumanResourceDTO dto = randomGenerate(userId, stage);
            humanResourceMapper.addHumanResource(dto);
        }

        // 查询并返回
        return humanResourceMapper.getTemp(userId)
                .stream()
                .map(this::convertToEntity)
                .toList();
    }

    private int randomLevel(Integer stage){
        int level;

        // 1:50% 2:20% 3:20% 4:10%
        if (PercentRandom.trueRandom(1,2)) level = 1;
        else if (PercentRandom.trueRandom(2,5)) level = 2;
        else if (PercentRandom.trueRandom(2,3)) level = 3;
        else level = 4;

        // 根据阶段，增加最低等级
        level += GameStageConst.HUMAN_RESOURCE_BONUS_LEVEL.getValue()[stage - 1];

        return level;
    }

    private HumanResourceDTO randomGenerate(Integer userId, Integer stage) {
        return new HumanResourceDTO(
                null,
                userId,
                "ToBeName",
                randomLevel(stage),
                GameStageConst.HUMAN_RESOURCE_MAX_LEVEL.getValue()[stage - 1],
                GameStageConst.HUMAN_RESOURCE_TRAIN_COST_EACH_LEVEL.getValue()[stage - 1],
                GameStageConst.HUMAN_RESOURCE_ACTION_POINT_EACH_LEVEL.getValue()[stage - 1],
                GameStageConst.HUMAN_RESOURCE_SALARY_EACH_LEVEL.getValue()[stage - 1]
        );
    }

    @Override
    public boolean sureHumanResource(HumanResourceSureDTO humanResourceSureDTO) {
        // 查询数据
        GameDataDTO gameData = gameDataMapper.getGameData(humanResourceSureDTO.getUserId());
        HumanResourceDTO dto = humanResourceMapper.getById(
                humanResourceSureDTO.getUserId(),
                humanResourceSureDTO.getId()
        );

        // 判断数据
        if(gameData.getGold() < GameStageConst.HUMAN_RESOURCE_RECRUIT_COST.getValue()[gameData.getLevel() - 1]) {
            return false;
        }

        // 扣除资金
        gameData.setGold(gameData.getGold() - GameStageConst.HUMAN_RESOURCE_RECRUIT_COST.getValue()[gameData.getLevel() - 1]);

        // 修改数据
        dto.setName(humanResourceSureDTO.getName());
        humanResourceMapper.sureHumanResource(dto);

        // 删除临时数据
        humanResourceMapper.deleteTemp(humanResourceSureDTO.getUserId());

        // 返回成功
        return true;
    }

    @Override
    public boolean levelUp(Integer userId, Integer id) {
        // 获取数据
        GameDataDTO gameData = gameDataMapper.getGameData(userId);
        HumanResourceDTO humanResource = humanResourceMapper.getById(userId, id);

        // 判断升级条件
        if (gameData.getGold() >= humanResource.getTrainCost() &&
                humanResource.getLevel() < humanResource.getMaxLevel()
        ) {
            // 更新数据
            gameData.setGold(gameData.getGold() - humanResource.getTrainCost());
            gameDataMapper.updateGameData(gameData);
            humanResource.setLevel(humanResource.getLevel() + 1);
            humanResourceMapper.updateHumanResource(humanResource);
            return true;
        } else {
            return false;
        }
    }
}
